package cn.com.bod.fas.pm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.com.bod.fas.pm.dao.mapper.CuCustomerMapper;
import cn.com.bod.fas.pm.dao.mapper.CuPerContactsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.bod.fas.pm.api.model.vo.dto.request.FeignCustBaseRequVo;
import cn.com.bod.fas.pm.api.model.vo.dto.request.FeignPerCustRequVo;
import cn.com.bod.fas.pm.api.service.FeignCuPerCustBaseService;
import cn.com.bod.fas.pm.dao.mapper.CuPerCustBaseMapper;
import cn.com.bod.fas.pm.dao.mapper.CuPerEntityMapper;
import cn.com.bod.fas.pm.model.po.CuCustomer;
import cn.com.bod.fas.pm.model.po.CuPerContacts;
import cn.com.bod.fas.pm.model.po.CuPerCustBase;
import cn.com.bod.fas.pm.model.po.CuPerEntity;
import cn.com.bod.fas.pm.model.vo.dto.request.CustBaseRequVo;
import cn.com.bod.fas.pm.service.CuPerCustBaseService;
import cn.com.bod.fas.pm.utils.DemoUtil;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.service.CommonService;
/**
 * @description 个人客户基本信息
 * @author houyaoxun
 * @date 2019年9月24日
 * @version 1.0.0
 * @Copyright (c) scfhdt
 */
@Service
public class CuPerCustBaseServiceImpl extends CommonService implements CuPerCustBaseService, FeignCuPerCustBaseService {
	
	/*
	 * 个人客户基本信息Mapper
	 */
    @Autowired
    private CuPerCustBaseMapper cuPerCustBaseMapper;
    
    /*
	 * 客户信息Mapper
	 */
    @Autowired
    private CuCustomerMapper cuCustomerMapper;
    
    /*
	 * 个人客户联系人Mapper
	 */
    @Autowired
    private CuPerContactsMapper cuPerContactsMapper;
    
    /*
	 * 个人客户经营实体Mapper
	 */
    @Autowired
    private CuPerEntityMapper cuPerEntityMapper;

    @Override
    protected CommonMapper<?> getMapper() {
        return cuPerCustBaseMapper;
    }
    /**
     * @description 新增个人客户
     * @author houyaoxun
     * @date 2019年9月16日
     * @param feignCustBaseRequVo
     * @return 客户编号
     */
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public String addPerCust(FeignCustBaseRequVo feignCustBaseRequVo) {
		// TODO 自动生成的方法存根
		/*
		 * 三要素查询是否客户已存在，如果存在返回客户编号
		 */
		CustBaseRequVo custBaseRequVo = DemoUtil.beanCopy(feignCustBaseRequVo, CustBaseRequVo:: new);
		String checkCustNo = cuCustomerMapper.getCustNo(custBaseRequVo);
		if(checkCustNo != null && !checkCustNo.isEmpty()) {
			return checkCustNo;
		};
		/*
		 * 生成客户信息实体类
		 */
		CuCustomer cuCustomer = DemoUtil.beanCopy(feignCustBaseRequVo, CuCustomer:: new);
		/*
		 * 生成个人客户基本信息实体类
		 */
		CuPerCustBase cuPerCustBase = new CuPerCustBase();
    	/*
    	 * 生成客户号
    	 */
    	Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyMMddHHmmss");
        String custNo = sdf.format(date);
        cuCustomer.setCustNo(custNo);
        cuPerCustBase.setCustNo(custNo);
		/*
		 * 个人客户基本信息新增
		 */
		cuPerCustBaseMapper.insertSelective(cuPerCustBase);
		/*
		 * 客户信息新增
		 */
		cuCustomerMapper.insertSelective(cuCustomer);
		
		return custNo;
	}
	
	/**
     * @description 新增个人客户详细信息
     * @author houyaoxun
	 * @param feignPerCustRequVo 客户对象
     * @date 2019年9月16日
     * @return void
     */
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public void addPerCustInfo(FeignPerCustRequVo feignPerCustRequVo) {
		// TODO 自动生成的方法存根
		/*
		 * 生成个人客户基本信息实体类
		 */
		CuPerCustBase cuPerCustBase = DemoUtil.beanCopy(feignPerCustRequVo.getFeignCuPerCustBaseRequVo(), CuPerCustBase:: new);
		/*
		 * 生成个人客户实体经营信息实体类
		 */
		CuPerEntity cuPerEntity = DemoUtil.beanCopy(feignPerCustRequVo.getFeignCuPerEntityRequVo(), CuPerEntity:: new);
		/*
		 * 生成个人客户联系人信息实体类集合
		 */
		List<CuPerContacts> cuPerContacts = DemoUtil.listCopy(feignPerCustRequVo.getFeignCuPerContactsRequVos(), CuPerContacts:: new);
		
	    	/*
			 * 个人客户基本信息新增
			 */
			cuPerCustBaseMapper.updateByPrimaryKeySelective(cuPerCustBase);
			/*
			 * 个人实体经营信息新增
			 */
			cuPerEntityMapper.insertSelective(cuPerEntity);
			/*
			 * 个人客户联系人信息新增
			 */
			for (CuPerContacts cuPerContact : cuPerContacts) {
				cuPerContactsMapper.insertSelective(cuPerContact);
			}
	}
}
